package com.x.crm.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.x.crm.model.Cooperation;
import com.x.crm.model.VO.CooperationVO;
import com.x.crm.rpc.CrmSysParamRpcService;
import com.x.crm.util.Constant;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/sysParam")
public class CooperationController extends BaseController{

	@Reference
	private CrmSysParamRpcService crmSysParamRpcService;


	@RequestMapping(value = "/selectCooperationList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCooperationList(Cooperation cooperation, CooperationVO cooperationVO) {
		if (cooperation.getShopId() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "Shopid不能为空！");
		}
		cooperation.setCooperationVO(cooperationVO);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectCooperationList(cooperation));
	}


	@RequestMapping(value = "/selectCooperationNum" , method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCooperationNum(Cooperation cooperation) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectCooperationNum(cooperation));
	}

	@RequestMapping(value = "/freezeOprCooperation" , method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel freezeCooperation(Cooperation cooperation) {
		if (cooperation.getCooperId() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "id不能为空！");
		}
		if (cooperation.getCooperState() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "状态不能为空！");
		}
		crmSysParamRpcService.updateCooperation(cooperation);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}


	@RequestMapping(value = "/insertOrUpdateCooperation", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertOrUpdateCooperation(Cooperation cooperation) {
		if (cooperation == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "合作机构信息不能为空！");
		}
		if (cooperation.getCooperName() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "合作机构名称不能为空！");
		}
		if (cooperation.getCoopertypeId() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "合作机构类型不能为空！");
		}
		if(this.isNotNull(cooperation.getCooperId())){
			int res = crmSysParamRpcService.updateCooperation(cooperation);
			if (res == 0){
				return new ResultModel(ResultCode.RESULT_FAIL, "更新失败");
			}
		}else{
			cooperation.setCooperState(Constant.COOPERATION_VALID);
			cooperation.setCreateTime(this.getDate(0));
			cooperation.setUpdateTime(this.getDate(0));
			crmSysParamRpcService.insertCooperation(cooperation);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}







}
